package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class Mhrmlimitholiday extends X_hrm_limitholiday {
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmlimitholiday.class);
	/**	 */
	private static final long serialVersionUID = 1536419696777266530L;
	/** limit vacations days a year*/
	public static final int LIMIT_VACATIONS = 30;

	public Mhrmlimitholiday(Properties ctx, int hrm_limitholiday_ID,
			String trxName) {
		super(ctx, hrm_limitholiday_ID, trxName);
	}

	public Mhrmlimitholiday(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}

	/** Load Model for limitholiday */
    public static Mhrmlimitholiday getLimitHoliday (int C_BPartner_ID, Boolean isActive)
    {
    	if(C_BPartner_ID == 0)
    		return null;
    	
    	String active = "";
    	
    	if(isActive == null)
    		active = "";
    	else if(isActive)
    		active = "Y";
    	else if(!isActive)
    		active = "AND HRM_LIMITHOLIDAY.ISACTIVE='N'";
    	
    	Mhrmlimitholiday hrmlim = new Mhrmlimitholiday(Env.getCtx(),0,null);
    	String sql_ = "";
    	PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		// Employee data from the card
		if(active.equals("Y")){
			sql_ = "SELECT * FROM HRM_LIMITHOLIDAY " +
						"WHERE ISACTIVE='" + active + "' \n" +
						"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n";
		}else{
			sql_ = "SELECT * FROM HRM_LIMITHOLIDAY " +
						"WHERE HRM_LIMITHOLIDAY.C_BPARTNER_ID=" + C_BPartner_ID + " \n" +
						"AND HRM_LIMITHOLIDAY.ENDDATE = (SELECT MAX(T.ENDDATE) FROM HRM_LIMITHOLIDAY T " +
														"WHERE T.C_BPARTNER_ID=HRM_LIMITHOLIDAY.C_BPARTNER_ID) " + active;
		}
		try
		{
			pstmt = DB.prepareStatement(sql_,null);
			rs = pstmt.executeQuery();
			if(rs.next())
				hrmlim = new Mhrmlimitholiday(Env.getCtx(), rs, null);			
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		return hrmlim;	
    }

    /**
     * Copy old values of From to new values of To.
     * set AD_Org_ID and set AD_Client_ID
     * @param from
     * @param to
     */
    public static void copyValuesLimit(Mhrmlimitholiday from, Mhrmlimitholiday to){
    	
    	PO.copyValues(from, to);
    	to.setAD_Org_ID(from.getAD_Org_ID());
    	to.setAD_Client_ID(from.getAD_Client_ID());    	
    }
    
}
